/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * This file is part of retabel.
 *
 * Copyright (c) 2011 by individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution
 * for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.retabel.web.rest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.core.MediaType;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * CDI REST implementation base class
 * <p/>
 * <h3>Extra-Info</h3>
 * Created: 16:21 10.06.11
 *
 * @author Peter Herber
 * @version 1.0
 */
public abstract class RestServiceBean {

    // constants

    private final static int DEFAULT_MAX_RESULTS_PER_PAGE = 10;

    // fields

    protected final Logger log = LoggerFactory.getLogger(getClass());

    // protected utility methods

    protected int getDefaultMaxResultsPerPage() {
        return DEFAULT_MAX_RESULTS_PER_PAGE;
    }



    protected boolean equalMediaTypes(MediaType mediaType1, MediaType mediaType2) {
        if (mediaType1 == null && mediaType2 == null) {
            return true;
        }

        if (mediaType1 == null) {
            return false;
        }

        // don't use equals, hence optional parameters send in the request wouldn't match the default text/plain type,
        // so we compare by isCompatible
        return mediaType1.isCompatible(mediaType2);
    }

    protected boolean containsMediaType(MediaType[] mediaTypes, MediaType mediaType) {
        if (mediaTypes == null) {
            return false;
        }

        for (MediaType comparedMediaType : mediaTypes) {
            if (comparedMediaType.isCompatible(mediaType)) {
                return true;
            }
        }

        return false;
    }

    protected byte[] streamToBytes(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        for (int c; (c = inputStream.read()) != -1; ) {
            byteArrayOutputStream.write(c);
        }

        return byteArrayOutputStream.toByteArray();
    }

}
